import 'package:appflutterhomeschool/api/api_service.dart';
import 'package:appflutterhomeschool/api/dio_utils.dart';
import 'package:appflutterhomeschool/common/common.dart';
import 'package:appflutterhomeschool/model/student_information_model.dart';
import 'package:appflutterhomeschool/view_model/my_app_view_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';

class RecipientListViewModel extends ChangeNotifier {
  BuildContext me;
  List<StudentModel> studentList = [];

  bool selectAll = false;

  RecipientListViewModel(this.me);

  initView() {}

  initData() {
    MyAppViewModel myAppViewModel = Provider.of<MyAppViewModel>(me);
    //  //获取学生列表
    DioUtils.getInstance().post(ApiService.getStudentByCls, {
      "token": Constant.getToken(),
      "cls_id": myAppViewModel.studentInformationModel.classId,
    }, (successCallBack) {
      studentList.clear();
      (successCallBack["data"]["student_list"] as List).forEach((element) {
        studentList.add(StudentModel.fromJson(false, element));
      });
      notifyListeners();
    }, (errorCallBack) {
      Fluttertoast.showToast(msg: errorCallBack);
    });
  }

  //点击事件
  studentOnTap(int index) {
    studentList[index].choice = !studentList[index].choice;
    allSelect();
  }

  //全选
  selectAllOnTap() {
//    studentList.followedBy((value){});
    if (selectAll) {
      for (int i = 0; i < studentList.length; i++) {
        studentList[i].choice = false;
      }
      selectAll = false;
    } else {
      selectAll = true;
      for (int i = 0; i < studentList.length; i++) {
        studentList[i].choice = true;
      }
    }
    notifyListeners();
  }

  //判断是否全部选择
  allSelect() {
    int index = 0;
    studentList.forEach((element) {
      if (element.choice) {
        index++;
      }
    });
    selectAll = studentList.length == index;
    notifyListeners();
  }
}
